using UnityEngine;

namespace Voodoo.Sauce.Tools.AccessButton
{
	public static class AccessProcess
	{
		public delegate void AccessButtonEvent();

		public static AccessButtonEvent InstantiateAccessButton;

		public static AccessButtonEvent DisposeAccessButton;

		public static GameObject ButtonInstance;

		private const string ACCESS_BUTTON_KEY = "accessButton";

		private const string TRUSTED_USER_KEY = "trustedUser";

		public static void SetTrustedUser(bool isTrusted)
		{
			NintendoPlayerPrefs.SetInt("trustedUser", isTrusted ? 1 : 0);
			SetAccess(isTrusted);
		}

		public static bool IsTrusted()
		{
			return NintendoPlayerPrefs.GetInt("trustedUser", 0) == 1;
		}

		public static void SetAccess(bool hasAccess)
		{
			NintendoPlayerPrefs.SetInt("accessButton", hasAccess ? 1 : 0);
			if (IsTrusted() && hasAccess)
			{
				CheckInstantiateButton();
			}
			else
			{
				CheckDisposeButton();
			}
		}

		public static bool HasAccess()
		{
			return NintendoPlayerPrefs.GetInt("accessButton", 0) == 1;
		}

		public static void CheckInstantiateButton()
		{
			if (!(ButtonInstance != null) && IsTrusted() && HasAccess())
			{
				InstantiateAccessButton?.Invoke();
			}
		}

		private static void CheckDisposeButton()
		{
			if (!(ButtonInstance == null))
			{
				DisposeAccessButton?.Invoke();
			}
		}
	}
}
